/**
 * XSAN 错误处理函数实现
 */

#include "include/xsan_error.h"
#include <errno.h>
#include <string.h>

/**
 * 将系统errno转换为xsan_error_t类型
 */
xsan_error_t xsan_error_from_errno(int err) {
    switch (err) {
        case 0:
            return XSAN_SUCCESS;
        case EINVAL:
            return XSAN_ERROR_INVALID_PARAM;
        case ENOMEM:
            return XSAN_ERROR_NO_MEMORY;
        case EIO:
            return XSAN_ERROR_IO;
        case ENOENT:
            return XSAN_ERROR_NOT_FOUND;
        case EEXIST:
            return XSAN_ERROR_ALREADY_EXISTS;
        case EACCES:
        case EPERM:
            return XSAN_ERROR_PERMISSION;
        case EBUSY:
            return XSAN_ERROR_BUSY;
        case ETIMEDOUT:
            return XSAN_ERROR_TIMEOUT;
        case ENOSYS:
        case EOPNOTSUPP:
            return XSAN_ERROR_NOT_SUPPORTED;
        case ENODEV:
            return XSAN_ERROR_DEVICE_ERROR;
        case ENETDOWN:
        case ENETUNREACH:
            return XSAN_ERROR_NETWORK;
        default:
            return XSAN_ERROR_UNKNOWN;
    }
}

/**
 * 获取错误描述字符串
 */
const char *xsan_error_to_string(xsan_error_t err) {
    switch (err) {
        case XSAN_SUCCESS:
            return "Success";
        case XSAN_ERROR_INVALID_PARAM:
            return "Invalid parameter";
        case XSAN_ERROR_NO_MEMORY:
            return "Out of memory";
        case XSAN_ERROR_IO:
            return "I/O error";
        case XSAN_ERROR_NOT_FOUND:
            return "Not found";
        case XSAN_ERROR_ALREADY_EXISTS:
            return "Already exists";
        case XSAN_ERROR_PERMISSION:
            return "Permission denied";
        case XSAN_ERROR_BUSY:
            return "Resource busy";
        case XSAN_ERROR_TIMEOUT:
            return "Timeout";
        case XSAN_ERROR_NOT_SUPPORTED:
            return "Not supported";
        case XSAN_ERROR_DEVICE_ERROR:
            return "Device error";
        case XSAN_ERROR_NETWORK:
            return "Network error";
        case XSAN_ERROR_CLUSTER:
            return "Cluster error";
        case XSAN_ERROR_NETWORK_DOWN:
            return "Network connection down";
        case XSAN_ERROR_CONNECTION_LOST:
            return "Connection lost";
        case XSAN_ERROR_SYSTEM:
            return "System error";
        case XSAN_ERROR_GENERIC:
            return "Generic error";
        case XSAN_ERROR_NOT_INITIALIZED:
            return "Module not initialized";
        case XSAN_ERROR_THREAD_CONTEXT:
            return "Thread context error";
        case XSAN_ERROR_INSUFFICIENT_SPACE:
            return "Insufficient space";
        case XSAN_ERROR_STORAGE_IO:
            return "Storage I/O error";
        case XSAN_ERROR_DEVICE_NOT_FOUND:
            return "Device not found";
        case XSAN_ERROR_VOLUME_NOT_FOUND:
            return "Volume not found";
        case XSAN_ERROR_CONFIG_PARSE:
            return "Configuration parse error";
        case XSAN_ERROR_CONFIG_INVALID:
            return "Invalid configuration";
        case XSAN_ERROR_TASK_FAILED:
            return "Task execution failed";
        case XSAN_ERROR_TASK_CANCELLED:
            return "Task cancelled";
        case XSAN_ERROR_VM_NOT_FOUND:
            return "VM not found";
        case XSAN_ERROR_UNKNOWN:
        default:
            return "Unknown error";
    }
}


